/******************************************************
*  Copyright(c) pages.ilovezxl.top
*  FileName : stdrope.h
*  Author : rivulet.dave
*  Description : 
*  Data : 2019-04-15
******************************************************/

#ifndef __STD_ROPE_H__
#define __STD_ROPE_H__

/*********************************************************************
 * INCLUDES
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/*********************************************************************
 *  EXTERNAL VARIABLES
 */

/*********************************************************************
 * TYPES
 */

#ifndef _GCC_WRAP_STDINT_H

typedef unsigned char         uint8_t;
typedef unsigned short        uint16_t;
typedef unsigned int          uint32_t;
typedef unsigned long long    uint64_t;

typedef signed char      int8_t;
typedef short int        int16_t;
typedef int              int32_t;
typedef long long        int64_t;

#endif  // _GCC_WRAP_STDINT_H 

typedef struct _uint8_array
{
    uint32_t uiSize;        /**< Number of array entries. */
    uint8_t *pucData;       /**< Pointer to array entries. */
} UINT8_ARRAY_S;

// 变长数组
typedef struct _var_array
{
    uint32_t uiLength;
    void *ptr;
} VAR_ARRAY_S;

typedef struct _module_information
{
    uint32_t uiModID;
    char *pName;
} MOD_INFO_S;

/*********************************************************************
 * MACROS
 */
#undef      _OK
#define     _OK       (0)

#undef      _ERR
#define     _ERR      (-1)

#undef      _IDLE
#define     _IDLE     (1)

#undef      _SUCC
#define     _SUCC     (0)

#undef      _FAIL
#define     _FAIL     (-1)

#define     _DISABLE 0
#define     _ENABLE     1

#define     _MIN(a, b)    ((a) < (b) ? (a) : (b))
#define     _MAX(a, b)    ((a) > (b) ? (a) : (b))

#define _BUILD_UINT32(Byte0, Byte1, Byte2, Byte3)      \
    ((uint32_t)((uint32_t)((Byte0) & 0x00FF)           \
                + ((uint32_t)((Byte1) & 0x00FF) << 8)  \
                + ((uint32_t)((Byte2) & 0x00FF) << 16) \
                + ((uint32_t)((Byte3) & 0x00FF) << 24)))

#define _BUILD_UINT16(loByte, hiByte) \
    ((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))

#define _HI_UINT16(a)    ((uint8_t)(((a) >> 8) & 0xFF))
#define _LO_UINT16(a)    ((uint8_t)((a) & 0xFF))

#define _BUILD_UINT8(hiByte, loByte) \
    ((uint8_t)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))

#define _HI_UINT8(a)     (((a) >> 4) & 0x0F)
#define _LO_UINT8(a)     ((a) & 0x0F)

#define _SWAP16(x)                                    \
    ((uint16_t)(                                      \
         (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
         (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))

#define _SWAP32(x)                                          \
    ((uint32_t)(                                            \
         (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
         (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) |  \
         (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) |  \
         (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))

#define _ARRAY_SIZE(a)               (sizeof((a)) / sizeof((a[0])))

#undef  _GET_BIT
#define _GET_BIT(var, mask)          ((var) & (mask))

#undef  _SET_BIT
#define _SET_BIT(var, mask)          ((var) |= (mask))

#undef  _CLEAR_BIT
#define _CLEAR_BIT(var, mask)        ((var) &= ~(mask))

/*
 *      »ñµÃ½á¹¹Ìå(TYPE)µÄ±äÁ¿³ÉÔ±(MEMBER)ÔÚ´Ë½á¹¹ÌåÖÐµÄÆ«ÒÆÁ¿¡£
 *      ËµÃ÷£º»ñµÃ½á¹¹Ìå(TYPE)µÄ±äÁ¿³ÉÔ±(MEMBER)ÔÚ´Ë½á¹¹ÌåÖÐµÄÆ«ÒÆÁ¿¡£
 * 1.( (TYPE *)0 ) ½«Áã×ªÐÍÎªTYPEÀàÐÍÖ¸Õë£¬¼´TYPEÀàÐÍµÄÖ¸ÕëµÄµØÖ·ÊÇ0¡£
 * 2.((TYPE *)0)->MEMBER    ·ÃÎÊ½á¹¹ÖÐµÄÊý¾Ý³ÉÔ±¡£
 * 3.&( ( (TYPE *)0 )->MEMBER )  È¡³öÊý¾Ý³ÉÔ±µÄµØÖ·¡£ÓÉÓÚTYPEµÄµØÖ·ÊÇ0£¬ÕâÀï»ñÈ¡µ½µÄµØÖ·¾ÍÊÇÏà¶ÔMEMBERÔÚTYPEÖÐµÄÆ«ÒÆ¡£
 * 4.(size_t)(&(((TYPE*)0)->MEMBER))  ½á¹û×ª»»ÀàÐÍ¡£¶ÔÓÚ32Î»ÏµÍ³¶øÑÔ£¬size_tÊÇunsigned intÀàÐÍ£»¶ÔÓÚ64Î»ÏµÍ³¶øÑÔ£¬size_tÊÇunsigned longÀàÐÍ¡£
 *
 * gender_offset = offsetof(struct student, gender);
 *
 */
#undef  _OFFSET_OF
#define _OFFSET_OF(TYPE, MEMBER)     (&((TYPE *)0)->MEMBER)

/*
 * µÃµ½Ò»¸ö½á¹¹ÌåÖÐfieldËùÕ¼ÓÃµÄ×Ö½ÚÊý
 */
#undef  _FIELD_SIZE
#define _FIELD_SIZE(TYPE, MEMBER)    sizeof(((TYPE *)0)->MEMBER)

#define _UNUSED(val)                 ((void)(val))

/* ºê×ª×Ö·û´® */
#define TOSTR(x)     #x

/* ×Ö·û´®³õÊ¼»¯ */
#define STR_ARRAY_INIT(a, l)   \
    do  \
    {   \
        a[0] = '\0';    \
        a[l-1] = '\0';  \
    }   \
    while (0)

/*********************************************************************
 * FUNCTIONS
 */

#ifdef __cplusplus
}
#endif /* end of __cplusplus */

#endif
/************************************** The End Of File **************************************/

